package com.lamer.console.base.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lamer.console.base.dto.UpdatePasswordParam;
import com.lamer.console.base.dto.UserDto;
import com.lamer.console.base.entity.Menu;
import com.lamer.console.base.entity.User;
import com.lamer.console.base.service.IBaseService;
import com.lamer.console.base.service.IMenuService;
import com.lamer.console.base.service.IUserService;
import com.lamer.console.common.api.ApiResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 黄诗鹏
 * @since 2021-09-25
 */
@RestController
@RequestMapping("/base")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    @Qualifier("UserServiceImpl")
    private IBaseService baseService;

    @Autowired
    private IMenuService menuService;

    @GetMapping("/user/page/")
    public ApiResult<Page<User>> getPage(@RequestParam("search") String search) {
        JSONObject jsonObject = JSON.parseObject(search);
        Integer number = jsonObject.getInteger("page_number");
        Integer size = jsonObject.getInteger("page_size");
        String userName = jsonObject.getString("userName");

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(userName)) queryWrapper.like(User::getUsername, userName);

        Page<User> page = userService.page(new Page<>(number, size), queryWrapper);
        return ApiResult.success(page);
    }

    @GetMapping("/user/{id}")
    public ApiResult<User> getData(@PathVariable("id") int id) {
        User data = userService.getById(id);
        return data == null ? ApiResult.failed() : ApiResult.success(data);
    }

    @PostMapping("/user/")
    public ApiResult<String> insertData(@RequestBody User data) {
        boolean flag = userService.postUser(data);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

    @PutMapping("/user/")
    public ApiResult<String> updateData(@RequestBody User data) {
        boolean flag = userService.putUser(data);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

    @DeleteMapping("/user/list/")
    public ApiResult<String> deleteData(@RequestParam("ids") int[] ids) {
        List<Integer> collect = Arrays.stream(ids).boxed().collect(Collectors.toList());
        boolean flag = userService.removeByIds(collect);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

    @PutMapping("/user/status/{id}")
    public ApiResult<String> updateStatusById(@PathVariable("id") int id){
        boolean flag = baseService.updateStatusById(id);
        return flag ? ApiResult.success() : ApiResult.failed();
    }

    @PutMapping("/user/password/update/{id}")
    public ApiResult<String> updatePasswordById(@PathVariable("id") int id, @RequestBody UpdatePasswordParam updatePasswordParam){
        boolean flag = userService.updatePassword(id, updatePasswordParam);
        return flag ? ApiResult.success("修改密码成功") : ApiResult.failed();
    }

    @PutMapping("/user/password/reset/{id}")
    public ApiResult<String> resetPasswordById(@PathVariable("id") int id){
        boolean flag = userService.resetPassword(id);
        return flag ? ApiResult.success("重置密码成功") : ApiResult.failed();
    }

    @GetMapping("/user/")
    public ApiResult<Map<String, Object>> getUserInfo(Principal principal){
        if(principal == null){
            return ApiResult.unauthorized();
        }
        String username = principal.getName();
        UserDto userDto = userService.getUserDtoByUserName(username);
        List<Menu> menus = menuService.getByUserId(userDto.getId());
        HashMap<String, Object> map = new HashMap<>();
        map.put("user", userDto);
        map.put("menus", menus);
        return ApiResult.success(map);
    }

}
